<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="出入库时间">
        <el-date-picker
          v-model="daterangeBusiDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.bus_ment_store_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.bus_ment_store_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item></el-form-item>
      <el-form-item label="仓库" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="请选择" clearable>
          <el-option
            v-for="data in storeList"
            :key="data.id"
            :label="data.storeName"
            :value="data.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['businessManagement:opInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['businessManagement:opInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['businessManagement:opInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['businessManagement:opInfo:export']"
        >导出</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd(0)"
          v-hasPermi="['businessManagement:opInfo:add']"
        >入库</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd(1)"
          v-hasPermi="['businessManagement:opInfo:add']"
        >出库</el-button>
      </el-col>
    </el-row>

    <el-table  border v-loading="loading" :data="opInfoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_ment_store_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="仓库" align="center" prop="storeName" />
      <el-table-column label="总数量" align="center" prop="amounts" />
      <el-table-column label="总金额" align="center" prop="totalCapital" />

      <el-table-column label="录入人" align="center" prop="operatorId" :formatter="getuser" />
      <el-table-column label="出入库时间" align="center" prop="busiDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.busiDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="录入时间" align="center" prop="operateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_ment_store_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column  width="200px"  label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['businessManagement:opInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['businessManagement:opInfo:remove']"
          >删除</el-button>-->

           
              <el-button
                size="small"
                @click="handleDelete(scope.row)"
                v-hasPermi="['businessManagement:opInfo:edit']"
                style="flex:1;margin-right:10%"
                :disabled="isEdit(scope.row)"
              >作废</el-button>

              <el-button
                size="small"
                @click="handleSelect(scope.row)"
                v-hasPermi="['businessManagement:opInfo:edit']"
                style="flex:1;margin-right:10%"
                
              >查看</el-button>
             
               <!-- 客户详细信息 -->
           <!--<el-popover
            placement="left"
            width="800"
            trigger="hover"
            @selection-change="handleSelectionChange"
          >
            <el-descriptions title="客户详细信息" direction="vertical" :column="4" border>
              <el-descriptions-item label="仓库" align="center">{{ scope.row.id }}</el-descriptions-item>
              <el-descriptions-item label="业务时间">{{ scope.row.customerName }}</el-descriptions-item>
              <el-descriptions-item label="备注" :span="2">{{scope.row.legalLeader}}</el-descriptions-item>
              <el-descriptions-item label="所属地区码">{{ scope.row.province}}</el-descriptions-item>
              <el-descriptions-item label="注册资本(万元)">{{ scope.row.regCapital }}</el-descriptions-item>
              <el-descriptions-item label="所属行业">{{ scope.row.scope }}</el-descriptions-item>
              <el-descriptions-item label="经营范围">{{ scope.row.industry }}</el-descriptions-item>
              <el-descriptions-item label="注册地址">{{ scope.row.regAddr }}</el-descriptions-item>
            </el-descriptions>
            <el-button slot="reference" 
            size="small">查看</el-button>
          </el-popover>
         -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出入库管理对话框
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出入库时间" prop="busiDate">
          <el-date-picker
            clearable
            v-model="form.busiDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出入库时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->
  </div>
</template>

<script>
import {
  listOpInfo,
  listOpInfoAndListInfo,
  getOpInfo,
  delOpInfo,
  delOpInfoLogical,
  addOpInfo,
  updateOpInfo
} from "@/api/businessManagement/opInfo";
import { listStores } from "@/api/businessManagement/store";
import { listUsers } from "@/api/system/user";
export default {
  name: "OpInfo",
  dicts: ["bus_ment_store_type", "bus_ment_store_status"],
  data() {
    return {
      // 所有的用户
      users: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入库管理表格数据
      opInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeBusiDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        storeId: null,
        busiDate: null,
        status: null
      },
      //仓库列表
      storeList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeId: [
          { required: true, message: "仓库id不能为空", trigger: "change" }
        ],
        busiDate: [
          { required: true, message: "出入库时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getUser();
    this.getStoreList();
    this.getList();
  },

  methods: {
    // 获取所有的用户信息
    getUser() {
      listUsers().then(res => {
        console.log(res);
        this.users = res.data;
      })
    },
    // 根据用户id获取名字
    getuser(row) {
      let userName = '';
      for (let user of this.users) {
        if (user.userId == row.operatorId) {
          userName = user.userName;
        }
      }
      return userName;
    },
    // 查看功能
    handleSelect(row) {
      let categoryPage = this.$router.resolve("/businessManagement/opInfo/op?billId="+row.id);
      window.open(
        categoryPage.href,
        "_blank",
        "height=600, width=1500,top=150,left=300"
      );
    },
    /** 查询正常状态的仓库 */
    getStoreList() {
      this.loading = true;
      listStores().then(response => {
        this.storeList = response.data;
        this.loading = false;
      });
    },
     // 是否可编辑
     isEdit(row) {
      // 获取当前状态
      let status = row.status;

      return !(status==0);
    },

    /** 查询出入库管理列表 */
    getList() {
      this.loading = true;
      /**联表查询 */
      listOpInfoAndListInfo(this.queryParams).then(response => {
        this.opInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        storeId: null,
        busiDate: null,
        status: null,
        operateDate: null,
        operatorId: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeBusiDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作
     * 0 入库
     * 1 出库
     */
    handleAdd(num) {
      this.reset();
      let categoryPage = this.$router.resolve("/businessManagement/opInfo/op?num="+num);
      window.open(
        categoryPage.href,
        "_blank",
        "height=600, width=1500,top=150,left=300"
      );
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOpInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出入库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOpInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOpInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作(逻辑删除) */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除出入库管理编号为"' + row.id + '"的数据项？')
        .then(function() {
          return delOpInfoLogical(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "businessManagement/opInfo/export",
        {
          ...this.queryParams
        },
        `opInfo_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
